#include <iostream>
using namespace std;
#define maxn 10005

int n;
int node_weight[maxn];
int endtime[maxn];
int maxans=0;

int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int u,v;
		cin>>u;
		cin>>node_weight[u];
		int maxpre=0;
		while(true){
			cin>>v;
			if(v==0) break;
			maxpre = max(maxpre, endtime[v]);
		}
		endtime[u] = maxpre+node_weight[u];
		maxans = max(maxans, endtime[u]);
	}
	cout<<maxans<<endl;
	return 0;
}
